package com.javastar;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.javastar.common.entity.BaseEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 旧版mp代码生成器
 *
 * @author zxx
 * @date 2023/7/24
 */
public class OldMyBatisPlusGenerator {
    private static String ip = "47.113.192.130";
    private static String database = "novel";
    private static String parent = "com.javastar";
    private static boolean swagger2 = true;
    private static String prefix = "";
    private static final String username = "root";
    private static final String password = "bEbXASrHseFZCwix";
    private static final String path = "Novel-admin";

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return List.of(
//                        "novel_author",
//                        "novel_block_show_novel",
//                        "novel_bookshelf",
//                        "novel_bookshelf_novel",
//                        "novel_category",
//                        "novel_chapter",
//                        "novel_chapter",
//                        "novel_comment",
//                        "novel_comment_like_user",
//                        "novel_comment_reply",
//                        "novel_comment_reply_like_user"
//                        "novel_index_recommend",
//                        "novel_novel",
//                        "novel_read_history",
//                        "novel_top",
//                        "novel_admin_operation_log",
                        "sys_user",
                        "sys_menu",
                        "sys_role",
                        "sys_role_user",
                        "sys_role_menu"
                ).stream().collect(Collectors.joining(","));
//                return getFileNames(path);
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    private static String getFileNames(String path) {
        File folder = new File(path);
        List<String> filenameList = Arrays.stream(folder.listFiles()).map(File::getName).toList();
        List<String> excludeNames = Arrays.asList(
                "novel_user"
        );
        filenameList.removeAll(excludeNames);
        return filenameList.stream().collect(Collectors.joining(","));
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir") + "/" + path;
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setServiceName("%sService");
        gc.setControllerName("%sController");
        gc.setServiceImplName("%sServiceImpl");
        gc.setMapperName("%sMapper");
        gc.setEntityName("%sEntity");
        gc.setAuthor("Java星球");
        gc.setOpen(false);
        gc.setSwagger2(swagger2);
        gc.setIdType(IdType.ASSIGN_ID);
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://" + ip + ":3306/" + database + "?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(parent);
        pc.setModuleName("modules");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        String templatePath = "/templates/mapper.xml.ftl";
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName().substring(0, tableInfo.getEntityName().length() - 6) + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
//        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
        strategy.setInclude(scanner("文件夹路径").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(prefix);
        strategy.setSuperEntityClass(BaseEntity.class);
        strategy.setEntitySerialVersionUID(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
